/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IMAGE_KNIFE_C_KEKE_IMAGEKNIFE_TASK_INTERNAL_H
#define IMAGE_KNIFE_C_KEKE_IMAGEKNIFE_TASK_INTERNAL_H

#include "RemoteCommunicationKit/rcp.h"
#include "imageknife_task.h"
#include "imageknife_request.h"
#include <cstdint>

namespace ImageKnifeC {

class ImageKnifeTaskInternal : public ImageKnifeTask {
public:
    std::string memoryKey;

    // 网络下载session，用于取消下载
    Rcp_Session *rcpSession = nullptr;
    // 网络下载request，用于取消下载
    Rcp_Request *rcpRequest = nullptr;
    // 网络下载, http响应数据
    Rcp_Response *rcpResponse = nullptr;

    ImageKnifeTaskInternal(ImageRequestType type, std::shared_ptr<ImageKnifeRequest> request,
        ImageSource *imageSrc) : type_(type), request_(request), imageSrc_(imageSrc)
    {
        SetIsAnimator();
    }

    ~ImageKnifeTaskInternal() override
    {
        if (rcpResponse != nullptr) {
                rcpResponse->destroyResponse(rcpResponse);
        }
    }

    Interceptor *GetCurrentInterceptor() const
    {
        return interceptor_;
    }

    void SetInterceptor(Interceptor *interceptor)
    {
        interceptor_ = interceptor;
    }

    void ClearInterceptorPtr()
    {
        interceptor_ = nullptr;
    }

    bool IsSuccess() const
    {
        return success_;
    }

    void MarkSuccess()
    {
        success_ = true;
    }

    void FatalError(std::string errorInfo)
    {
        fatalError_ = true;
        EchoError("[Fatal Error] " + errorInfo);
    }

    void EchoError(std::string errorInfo) override
    {
        std::string source;
        if (interceptor_ != nullptr) {
            source = " (" + interceptor_.load()->name + ") ";
        }
        lastError_ = GetTaskInfo(type_, request_.get()) + errorInfo + source;
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_DOMAIN, "TaskError", "%{public}s", lastError_.c_str());
    }

    std::string GetLastError() const
    {
        return lastError_;
    }

    bool IsFatalErrorHappened()
    {
        return fatalError_;
    }

    static std::string GetTaskInfo(ImageRequestType type, ImageKnifeRequest *request);

    void QueryDisplayPixelmapInfo();

    std::string GetDisplayPixelmapInfo() const;

    bool IsAnimator() const override
    {
        return isAnimator_;
    }

    bool IsAnimationImage() const
    {
        return isAnimationImage_;
    }

    ImageSource *GetImageSource() const override;

    ImageRequestType GetImageRequestType() const override;

    std::shared_ptr<ImageKnifeRequest> GetImageKnifeRequest() const override;

    ImageFormat GetImageBufferFormat() const override;
    void QueryImageBufferFormat();
private:
    struct PixelmapInfo {
        uint32_t width = 0;
        uint32_t height = 0;
        size_t  size = 0;
        std::string pixelFormat = "Unknown";
    };

    PixelmapInfo pixelmapInfo;

    std::shared_ptr<ImageKnifeRequest> request_ = nullptr;
    ImageRequestType type_ = ImageRequestType::MAIN_SRC;
    ImageSource *imageSrc_ = nullptr;

    std::atomic<Interceptor*> interceptor_ = nullptr;
    bool success_ = false;
    bool fatalError_ = false;
    std::string lastError_;

    // 组件是否为Animator
    bool isAnimator_ = false;
    // 图片source是否为动图类型
    bool isAnimationImage_ = false;
    ImageFormat imageBufferFormat_ = ImageFormat::UNKNOWN;
    void SetIsAnimator();
};
} // end of namespace
#endif // IMAGE_KNIFE_C_KEKE_IMAGEKNIFE_TASK_INTERNAL_H
